package org.jeecg.modules.gqHealth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.gqHealth.entity.GqCardManager;
import org.jeecg.modules.gqHealth.entity.GqCheckUpRecord;
import org.jeecg.modules.gqHealth.entity.GqHospitalInfo;
import org.jeecg.modules.gqHealth.mapper.GqCardManagerMapper;
import org.jeecg.modules.gqHealth.mapper.GqCheckUpRecordMapper;
import org.jeecg.modules.gqHealth.mapper.GqHospitalInfoMapper;
import org.jeecg.modules.gqHealth.service.IGqCheckUpRecordService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @Description: 用户体检记录表
 * @Author: jeecg-boot
 * @Date:   2024-05-12
 * @Version: V1.0
 */
@Service
public class GqCheckUpRecordServiceImpl extends ServiceImpl<GqCheckUpRecordMapper, GqCheckUpRecord> implements IGqCheckUpRecordService {

    @Resource
    private GqCheckUpRecordMapper mapper;

    @Resource
    private GqCardManagerMapper cardManagerMapper;


    @Resource
    private GqHospitalInfoMapper hospitalInfoMapper;

    @Override
    public Integer checkUpCard(String no) {
        QueryWrapper<GqCardManager> wrapper = new QueryWrapper<>();
        wrapper.eq("card_no",no);
        GqCardManager gqCardManager = cardManagerMapper.selectOne(wrapper);
        if (null == gqCardManager || StringUtils.isEmpty(gqCardManager.getId())){
            return  -1;
        }
        if (null == gqCardManager.getActiveTime()){
            //卡未激活
            return  -2;
        }
        if ( null != gqCardManager.getUseNum() && gqCardManager.getUseNum().intValue() >= gqCardManager.getNum()){
            //卡已使用完
            return  -3;
        }
        LocalDate threeMonthsLater =LocalDate.now();
        Date date = Date.from(threeMonthsLater.atStartOfDay(ZoneId.systemDefault()).toInstant());

        if ( gqCardManager.getEndTime().compareTo(date) < 0){
            //卡已过期
            return  -4;
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<GqHospitalInfo> wb = new QueryWrapper<>();
        wb.eq("account",sysUser.getUsername());
        List<GqHospitalInfo> gqHospitalInfos = hospitalInfoMapper.selectList(wb);
        if (null == gqHospitalInfos || gqHospitalInfos.isEmpty()){
            //账户不是医院
            return -5;
        }
        GqCheckUpRecord bean = new GqCheckUpRecord();
        bean.setCardNo(no);
        bean.setYyId(sysUser.getUsername());
        if (save(bean)){
            if (null == gqCardManager.getUseNum()){
                gqCardManager.setUseNum(0);
            }
            gqCardManager.setUseNum(gqCardManager.getUseNum()+1);
            cardManagerMapper.updateById(gqCardManager);
            return 1;
        }
        return -6;
    }

    @Override
    public IPage<GqCheckUpRecord> selectRecord(String startTime, String endTime, String account, Page<GqCheckUpRecord> page) {

        return mapper.selectRecord(page,startTime,endTime,account);
    }

    @Override
    public IPage<GqCheckUpRecord> selectRecord(String account, Page<GqCheckUpRecord> page) {
        return mapper.selectUserRecord(page,account);
    }
}
